package com.itheima.leetcode;

import com.itheima.queue.LinkedListQueue;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 二叉树层序遍历
 *
 * @FileName E01Leetcode102
 * @Description
 * @Author Sword
 * @date 2025-04-22
 **/
public class E01LeetCode102 {

    /*
        {
            {1}
            {2,3}
            {4,5,6,7}
        }
     */
    public static List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> result = new LinkedList<>();

        if (root == null) {
            return result;
        }

        LinkedListQueue<TreeNode> queue = new LinkedListQueue<>();
        queue.offer(root);

        int c1 = 1; //当前层节点数

        while (!queue.isEmpty()) {
            List<Integer> level = new ArrayList<>();    //保存每一层结果

            int c2 = 0; //下层节点数
            for (int i = 0; i < c1; i++) {
                TreeNode n = queue.poll();
                level.add(n.val);

                if (n.left != null) {
                    queue.offer(n.left);
                    c2++;
                }

                if (n.right != null) {
                    queue.offer(n.right);
                    c2++;
                }
            }

            result.add(level);
            c1 = c2;
        }
        return result;
    }

    /*
                             1
                            / \
                           2   3
                          /\   /\
                         4  5 6  7
     */

    public static void main(String[] args) {
        TreeNode root = new TreeNode(
                new TreeNode(
                        new TreeNode(4),
                        2,
                        new TreeNode(5)
                ),
                1,
                new TreeNode(
                        new TreeNode(5),
                        3,
                        new TreeNode(6)
                )
        );

        List<List<Integer>> lists = levelOrder(root);
        System.out.println(lists);

        /*LinkedListQueue<TreeNode> queue = new LinkedListQueue<>();
        queue.offer(root);

        int c1 = 1; //当前层结点数
        while (!queue.isEmpty()) {
            int c2 = 0; //下层结点数
            for (int i = 0; i < c1; i++) {
                TreeNode n = queue.poll();
                System.out.print(n + " ");
                if (n.left != null) {
                    queue.offer(n.left);
                    c2++;
                }

                if (n.right != null) {
                    queue.offer(n.right);
                    c2++;
                }
            }
            System.out.println();
            c1 = c2;
        }*/
    }
}
